﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Main_UserLogin : ValidateBases
{
    UsersDao ud = new UsersDao();
    UsersVo uv = new UsersVo();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.txtupass.Attributes.Add("value", "123456");
        if (!Page.IsPostBack)
        {
            if (Session["user"] != null)
            {
                uv = (UsersVo)Session["user"];
                if (uv != null)
                {
                    uv = ud.getUserById(uv.Uid.ToString());
                    Panel1.Visible = false;
                    Panel2.Visible = true;
                    lblname.Text = uv.Unickname;
                    lbldate.Text = Convert.ToDateTime(uv.Ulastvisit).ToString("yyyy-MM-dd") ;
                    lbljifen.Text = uv.Umoney.ToString();
                    if (uv.Ugroup == "店家用户")
                    {
                        lbupdate.Text = "更新资料";
                    }
                }
            }
        }

    }
    protected void btnlogin_Click(object sender, EventArgs e)
    {
        int count = 0;
        ArrayList list = new ArrayList();
        list.Add("<");
        list.Add(">");
        list.Add(";");
        for (int i = 0; i < txtupass.Text.Length; i++)
        {
            if (list.Contains(txtupass.Text[i].ToString()))
            {
                count = -1;
            }
        }
        if (count < 0)
        {
            string script = "alert('请检查用户名或密码！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click1", script, true);
            return;
        }
        if (txtuname.Value == ""||txtupass.Text == "")
        {
            string script = "alert('用户名密码不能为空！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
            return;
        }
        if (txtVail.Value == "")
        {
            string script = "alert('验证码不能为空！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
            return;
        }
        if (txtVail.Value == base.strValidate)
        {
            uv = ud.UsersLogin(txtuname.Value, txtupass.Text);
            if (uv != null)
            {

                if (uv.Ugroup == "管理员")
                {
                    Session.Add("user", uv);
                    Session.Add("admin", uv);
                    string script = "location.href='Main/NewsList.aspx';";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
                }
                else
                {
                    Session.Add("user", uv);
                    string script = "location.href='index.aspx';";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
                }
            }
            else
            {
                string script = "alert('请检查用户名或密码！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click1", script, true);
                txtuname.Value = "";
                txtVail.Value = "";
            }
        }
        else
        {
            string script = "alert('验证码错误！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
            txtuname.Value = "";
            txtVail.Value = "";
        }
    }
    protected void lbtexit_Click(object sender, EventArgs e)
    {
        Session.Remove("user");
        Response.Redirect("index.aspx");
    }
    protected void lbupdate_Click(object sender, EventArgs e)
    {
         uv = (UsersVo)Session["user"];
         if (uv.Ugroup == "店家用户")
         {
             Response.Redirect("Main/ShopManage.aspx");
         }
         else 
         {
             Response.Redirect("Main/UserUpdate.aspx");
         }
    }
    protected void lbpost_Click(object sender, EventArgs e)
    {
        Response.Redirect("SendMessage.aspx");
    }
    protected void lbedit_Click(object sender, EventArgs e)
    {
        Response.Redirect("Main/MyMessage.aspx");
    }
    protected void btnshopreg_Click(object sender, EventArgs e)
    {
        Response.Redirect("Main/ShopRegister.aspx");
    }
}
